/**
 * 
 */
package vn.smartware.co.framework.validator;

import java.text.MessageFormat;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.apache.commons.validator.Arg;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;

import vn.smartware.co.framework.exception.ClientException;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.plugin.FileResource;


/**
 * @author thuctt
 * Resources
 */
public class Resources {
	
	/*************************************************************************
	 * Description:
	 * 
	 * @param va
	 * @param field
	 * @return ActionError
	 *************************************************************************/
	public static ActionError getActionError(ValidatorAction va, Field field) {
		
		String args[] = getArgs(va.getName(), field);
		
		String msg = field.getMsg(va.getName()) != null ? 
				     field.getMsg(va.getName()) : va.getMsg();
				     
	    return new ActionError(msg, args);
	}
	
	/*************************************************************************
	 * ServerApplicationResource.properties
	 * 
	 * @param message
	 * @param context
	 * @return message
	 * @throws ServerException 
	 *************************************************************************/	
	public static String getMessage(ActionMessage message, ServletContext context)
	                                  throws ServerException {
		
		String propMessage = null;
		
		Properties prop = FileResource.getInstance(context).getMessageResource();

		if (prop != null) {
			String key = message.getKey();
			
			if (key != null) {
				propMessage = prop.getProperty(message.getKey());
			}
		}
		
		if (propMessage != null) {
			return MessageFormat.format(propMessage, message.getValues());
		}
		return null;
	}
	
	/*************************************************************************
	 * ClientApplicationResource.properties
	 * 
	 * @param message
	 * @return message
	 * @throws ClientException 
	 *************************************************************************/
	public static String getMessage(ActionMessage message) throws ClientException {
		
		String propMessage = null;
		
		Properties prop = FileResource.getInstance().getMessageResource();

		if (prop != null) {
			String key = message.getKey();
			
			if (key != null) {
				propMessage = prop.getProperty(message.getKey());
			}
		}
		
		if (propMessage != null) {
			return MessageFormat.format(propMessage, message.getValues());
		}
		return null;
	}
	
	/*************************************************************************
	 * Description:
	 * Gets the message arguments based on the current
	 * @param actionName
	 * @param field
	 * @return argMessage
	 * 2005/02/21
	 *************************************************************************/
	public static String[] getArgs(String actionName, Field field) {
		
		String [] argMessages = new String[4];
		
		Arg[] args = new Arg[] {
				                 field.getArg(actionName, 0), 
								 field.getArg(actionName, 1), 
								 field.getArg(actionName, 2), 
								 field.getArg(actionName, 3)};
		
		for (int i = 0; i < args.length; i++) {
			if (args[i] == null) {
				continue;
			}
			
			argMessages[i] = args[i].getKey();
		}
		
		return argMessages;
	}

	/*************************************************************************
	 * Description:
	 * 
	 * @param va
	 * @param field
	 * @return ActionRight
	 *************************************************************************/
	public static ActionRight getActionRight(ValidatorAction va, Field field) {
		
		final String MSG_RIGHT = "right.mask";
		
		String args[] = getArgs(va.getName(), field);
		
		String msg = field.getMsg(va.getName()) != null ? 
					 field.getMsg(va.getName()) : MSG_RIGHT;
		
		return ActionRight.getInstance(msg, args);
	}
}
